﻿using System.Drawing;

namespace AIthin.Model
{
    public class Squash : Reality
    {
        /// <summary>
        /// "Schläger" als Rektangle zu beschreiben
        /// </summary>
        public static Rectangle p;
        /// <summary>
        /// "Ball" der springt rum
        /// </summary>
        public static Ball b;
        /// <summary>
        /// "Zimmer" wo Ball springt
        /// </summary>
        public Room room;
        /// <summary>
        /// Konstruktor für Reality
        /// </summary>
        public Squash(int hight, int width)
            : base(hight, width)
        {
            
            room = new Room(width, hight);
            b = new Ball();
            ShootBall();
            // Schläger
     //       p = new Rectangle(32, room.Y + room.H - 4, 10, 5);
        }
        /// <summary>
        /// Zeichnet alle Elemente.
        /// </summary>
        /// <param name="g">GDI+ Zeichnungsoberfläche</param>
        public override void Draw(Graphics g)
        {
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //draw back color
            g.FillRectangle(Brushes.White, room.X, room.Y, room.W, room.H);
            //draw ball
            g.FillEllipse(Brushes.Black, b.X, b.Y, b.Size, b.Size);
            //draw walls
            g.DrawLine(Pens.Black, room.X, room.H + room.Y, room.X, room.Y);
            g.DrawLine(Pens.Black, room.X, room.Y, room.X + room.W, room.Y);
            g.DrawLine(Pens.Black, room.X + room.W-1, room.Y, room.X + room.W-1, room.H + room.Y);
        }
        public override void Draw(ref short[,] g)
        {

            for (int i = 0; i < 64; i++)
                for (int j = 0; j < 64; j++)
                {
                    g[i, j] = 0;
                }

            //draw ball

            for (int i = 0; i < 6; i++)
                for (int j = 0; j < 6; j++)
                {
                    if (b.X + i > 0 & b.X + i < 64 & room.H - b.Y > 0 & room.H - b.Y < 64)
                        g[b.X + i, room.H - b.Y] = 1;
                }
         }
        /// <summary>
        /// Aufnimmt für Schläger die Bewegungen von dem Maus
        /// </summary>
        /// <param name="e"> Mouse Even Arguments</param>
        public void Influense(System.Windows.Forms.MouseEventArgs e)
        {
            if (p.X < 0)
            {
                p.X = 0;
            }
            else
            {
                p.X = e.X;
            }
        }
        /// <summary>
        /// Schießt Ball von unten
        /// </summary>
        public override void ShootBall()
        {
            int Range = 50;
            b.X = base.RandomValue.Next(
                room.X + room.W / 2 - Range/2,
                room.X + room.W / 2 + Range / 2);
            b.Y = room.H;
            b.Vx = base.RandomValue.Next(0, 0);
            b.Vy = base.RandomValue.Next(1, 2);
        }
        /// <summary>
        /// Modelliert absprung von den Wänden des Zimmer
        /// </summary>
        public override bool Moving()
        {
            // wenn Ball ueber vertikale wand 
            if (b.X <= room.X | b.X >= (room.X + room.W - b.Size))
                //richtung aendern
                b.Vx = -b.Vx;

            // weiter fliegen
            b.X += b.Vx;
            // minus wegen koordinaten Y
            b.Y -= b.Vy;

            /*
                        if (b.Y > room.Y)
                        {
                            b.Y -= b.Vy;
                        }
                        else
                        {
                            b.Vy = -b.Vy;
                            b.Y -= b.Vy;
                        }
             */
            // wenn fliegt raus
            if (b.Y < room.Y)
            {
                this.ShootBall();
                return true;
            }
            return false;
        }
        public override int GetBallPositionX()
        {
            return b.X + b.Size / 2;
        }
        public override int GetBallPositionY()
        {
            return b.Y + b.Size / 2;
        }
    }
}
